﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Parallel3M.parallel3m.protocol;
using System.Xml;

namespace Parallel3M.parallel3m.framework
{
    public class ParallelProtocolTransfer:ParallelTransfer
    {
        /*
        <payload timestamp='' pcss='' pcsd='' routeTag='*' domain='protocolname' type='request' routeCount='2' handlerid='CONNECT' access='Public' language='c#'>
            <request>
                <username></username>
         *      <password></password>
         *      <pcsid></pcsid>
             </request>
             <response/>
         </payload>
        */

        public override ArrayList execute(ParallelPayload payload)
        {
            ArrayList payloads = new ArrayList();
            
            String protocolName = payload.domain;
            ProtocolProtype protocol = (ProtocolProtype)ParallelProtocolContext.protocols[protocolName];
            string protocolCommand = payload.handlerid;            
            ProtocolHandler protocolHandler = (ProtocolHandler)protocol.protocolHandlers[protocolCommand];

            if (protocolHandler != null)
            {
                payloads = protocolHandler.handler(payload);                             
            }
            return payloads;
        }


        public override void FlowoutPolicy(ArrayList payloads, ArrayList outputConnections)
        {
            if (payloads != null)
            {
                foreach (ParallelPayload output_payload in payloads)
                {
                    if (output_payload.routeCount == 0)
                    {
                        foreach (ParallelConnection outputConnection in outputConnections)
                        {
                            if (outputConnection.routeTag == "0")
                            {
                                outputConnection.Flowin(output_payload);
                                break;
                            }

                        }
                    }
                    else
                    {
                        foreach (ParallelConnection outputConnection in outputConnections)
                        {
                            outputConnection.Flowin(output_payload);
                            break;

                        }
                    }
                }
            }

        }

    }
}
